import { Base64 } from 'js-base64'
// import {isDebugMode} from '@/config/public'
const isDebugMode = true

export default {
  namespaced: true,
  state: {
    user: undefined,
    permissions: null,
    roles: null,
    routesConfig: null
  },
  getters: {
    user: state => {
      if (!state.user) {
        try {
          const user = localStorage.getItem(process.env.VUE_APP_USER_KEY)
          state.user = isDebugMode
            ? JSON.parse(user)
            : Base64.decode(JSON.parse(user))
          state.user = state.user ? state.user : {}
        } catch (e) {
          console.error(e)
        }
      }
      return state.user
    },
    permissions: state => {
      if (!state.permissions) {
        try {
          const permissions = localStorage.getItem(
            process.env.VUE_APP_PERMISSIONS_KEY
          )
          state.permissions = isDebugMode
            ? JSON.parse(permissions)
            : Base64.decode(JSON.parse(permissions))
          state.permissions = state.permissions ? state.permissions : []
        } catch (e) {
          console.error(e.message)
        }
      }
      return state.permissions
    },
    roles: state => {
      if (!state.roles) {
        try {
          const roles = localStorage.getItem(process.env.VUE_APP_ROLES_KEY)
          state.roles = isDebugMode
            ? JSON.parse(roles)
            : Base64.decode(JSON.parse(roles))
          state.roles = state.roles ? state.roles : []
        } catch (e) {
          console.error(e.message)
        }
      }
      return state.roles
    },
    routesConfig: state => {
      if (!state.routesConfig) {
        try {
          const routesConfig = localStorage.getItem(
            process.env.VUE_APP_ROUTES_KEY
          )
          state.routesConfig = isDebugMode
            ? JSON.parse(routesConfig)
            : Base64.decode(JSON.parse(routesConfig))
          state.routesConfig = state.routesConfig ? state.routesConfig : []
        } catch (e) {
          console.error(e.message)
        }
      }
      return state.routesConfig
    }
  },
  mutations: {
    setUser(state, user) {
      state.user = user
      localStorage.setItem(
        process.env.VUE_APP_USER_KEY,
        isDebugMode ? JSON.stringify(user) : Base64.encode(JSON.stringify(user))
      )
    },
    setPermissions(state, permissions) {
      state.permissions = permissions
      localStorage.setItem(
        process.env.VUE_APP_PERMISSIONS_KEY,
        isDebugMode
          ? JSON.stringify(permissions)
          : Base64.encode(JSON.stringify(permissions))
      )
    },
    setRoles(state, roles) {
      state.roles = roles
      localStorage.setItem(
        process.env.VUE_APP_ROLES_KEY,
        isDebugMode
          ? JSON.stringify(roles)
          : Base64.encode(JSON.stringify(roles))
      )
    },
    setRoutesConfig(state, routesConfig) {
      state.routesConfig = routesConfig
      localStorage.setItem(
        process.env.VUE_APP_ROUTES_KEY,
        isDebugMode
          ? JSON.stringify(routesConfig)
          : Base64.encode(JSON.stringify(routesConfig))
      )
    }
  }
}
